



-- TODO
-- Add a way to write files
-- Implement loading (easy)
-- test

function printCentred(text, line)
    screenWidth = 100
    screenHeight = 64
	y = line*8
	len = string.len(text)
	startPoint = screenWidth/2 - (len*6)/2
	nxt.DisplayText(text, startPoint, y)
end

-- Elements is array of strings (names) 
-- Selected is id of selected row, from 1 (0 = no selection)
-- Scroll is starting element, starts at 1
function displayList(elements, selected)
	nxt.DisplayClear()
	maxL = (screenHeight - 1)/8
	scroll = 1
	if selected >= maxL then
		scroll = maxL
	end
	for i = scroll, scroll+((screenHeight - 1)/8), 1 do
		if elements[i] == nil then
			break
		end
		if i == selected then
			printCentred("> "..elements[i].." <", (i - scroll))
		else
			printCentred(elements[i], (i - scroll))
		end	
	end
end


    
-- Loads and runs a file
function runfile(name)
	nxt.DisplayClear()
	if not nxt.FileExists(name) then
		return false
	end
	f = nxt.loadfile(name)
	f()
	while nxt.ButtonRead() == 0 do
		printCentred("Program Finished", 4)
		printCentred("Press any Key", 4)
	end
end

function displayMenuScreen(files)
	-- Dummy files
	--files = {"hello", "world", "coolstuff", "hello", "world", "coolstuff", "hello", "world", "coolstuff", "hello"}
        
	count = 0
	for file in pairs(files) do
		count = count + 1
	end
	loc = 1
	while nxt.ButtonRead() ~= 1 do
		if nxt.ButtonRead() == 2 then
			loc = loc + 1
			if loc > count then
				loc = count
			end
		end
		if nxt.ButtonRead() == 4 then
			loc = loc - 1
			if loc < 1 then
				loc = 1
			end
		end
		if nxt.ButtonRead() == 8 then
			return loc
		end
		displayList(files, loc)
		timen = nxt.TimerRead()
		repeat
		until nxt.TimerRead() > timen + 250
	end
	return 0
end

function boot()
    -- fancy boot script because Y0l0
    nxt.DisplayClear()
    onBoot = nxt.TimerRead()
    repeat
        printCentred("Booting", 2)
    until nxt.TimerRead() > onBoot + 1000
    nxt.DisplayClear()
        
    repeat
        printCentred("Booting - ", 2)
    until nxt.TimerRead() > onBoot + 2000
    nxt.DisplayClear()
        
    repeat
        printCentred("Booting -- ", 2)
    until nxt.TimerRead() > onBoot + 3000
    nxt.DisplayClear()    
    
    displayMenuScreen()
    
    -- add the file chosen to a function and run it\
end

    function splitString(str)
    str = string.gsub(str, "%s+", "")
        end
    end

-- Python-style string splitting






lol =0
names = {}
for key,value in next, string.split(nxt.FileHandleInfo(h), ' ') do 
    names{key} = value
    print(names[2]) 
end






raw = {}
for h=1,31 do
    raw[h] = "a"
    raw[h] = nxt.FileHandleInfo(h)
    print (raw[h])
end

for lol = 1,32 do
    
end
    




function sV()
    for h=1,10 do
        kek = nxt.FileHandleInfo(h)
        lol = split(kek," ")
        print(lol)
    end
end

-- don't copy past here ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
-- this is broked

-- this doesn't work right now
function gB()
    mV, recharge, cap = nxt.BatteryRead()
    print("Volts" .. mv)
    print(" - R:" .. recharge)
    print("Cap" .. cap)
end

-- //]]

-- Create the file, save the handle so we can use it later...
h = nxt.FileCreate( "hello-world", 128 )

-- Now write the string...
nxt.FileWrite( h, programString )

-- And close the file...
nxt.FileClose( h )


function dumpDisplay()
  for x=0,99 do
    s = ""
    for y=0,63 do
      if 0 ~= nxt.DisplayGetPixel(x,63-y) then
        s = s .. "*"
       else
        s = s .. " "
      end
    end
    print( s )
  end
end





